<template>
	<!-- 打卡日历页面 -->
	<view class="daka">
		<view class='all'>
			<view class="bar">
				<!-- 上一个月 -->
				<view class="previous" @click="handleCalendar(0)">
					<button class="barbtn">上一月</button>
				</view>
				<!-- 显示年月 -->
				<view class="date">{{cur_year || "--"}} 年 {{cur_month || "--"}} 月</view>
				<!-- 下一个月 -->
				<view class="next" @click="handleCalendar(1)">
					<button class="barbtn">下一月</button>
				</view>
			</view>
			<!-- 显示星期 -->
			<view class="week">
				<view class="week_ri" v-for="(item,index) in weeks_ch" :key="index">{{item}}</view>
			</view>
			<view class="myDateTable">
				<view v-for="(item,j) in days" :key="j" class='dateCell'>
					<view v-if="item.date==undefined || item.date == null" class='cell'>
						<text :decode="true">&nbsp;&nbsp;</text>
					</view>
					<view v-else @click="clickSignUp(item)">
						<!-- 不可补卡 -->
						<view v-if="item.isSign == 5" class='cell yellow' style="background: #999999;color: #ffffff;">
							<text>{{item.date}}</text>
						</view>
						<!-- 可补卡 -->
						<view class="cell white bg-red" style="background: #F57B61;color: #ffffff;"
							v-else-if="item.isSign == 2">
							<text>{{item.date}}</text>
						</view>
						<!-- 今日日期 -->
						<view class="cell white bg-red" style="background: #61F58B;color: #ffffff;"
							v-else-if="item.isSign == 3">
							<text>{{item.date}}</text>
						</view>
						<!-- 当前日期之后 -->
						<view class="cell white bg-red" style="color: #666;" v-else-if="item.isSign == 4">
							<text>{{item.date}}</text>
						</view>
						<!-- 否则 -->
						<view class="whiteColor cell" v-else>
							<text>{{item.date}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 设置提示打卡 -->
		<view class="flexcenter">
			<view class="flexleft">
				<view class="huangyuan"></view>
				<view class="bktext">可补卡</view>
			</view>
			<view class="flexleft">
				<view class="lvyuan"></view>
				<view class="bktext">待打卡</view>
			</view>
			<view class="flexleft">
				<view class="huiyuan"></view>
				<view class="bktext">不可补卡</view>
			</view>
		</view>
		<view class="dibu">
			<view class="bktext" style="text-align: center;">注：未打卡请尽快补卡，一周未补卡则不可补卡！！！</view>
			<view class="tuiku mainpadding margin_top" v-if="!is_daka" @click="startdaka">上传今日打卡内容</view>
			<view class="tuiku huanse mainpadding margin_top"  v-if="is_daka">今日打卡任务已完成</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				days: [], //日历
				SignUp: [],
				cur_year: 0, //当前选的年
				cur_month: 0, //当前选的月
				today: parseInt(new Date().getDate()), //今天
				toMonth: parseInt(new Date().getMonth() + 1), //本月
				toYear: parseInt(new Date().getFullYear()), //本年
				weeks_ch: ['一', '二', '三', '四', '五', '六', '日'],
				zidingy: [],
				is_daka:false
			};
		},
		props: {
			//年
			sendYear: {
				type: Number,
				default: new Date().getFullYear()
			},
			//月
			sendMonth: {
				type: Number,
				default: new Date().getMonth() + 1
			},
		},
		computed:{
			
		},
		onLoad() {
			this.cur_year = this.sendYear; //年
			this.cur_month = this.sendMonth; //月
			this.calculateEmptyGrids(this.cur_year, this.cur_month);
			this.calculateDays(this.cur_year, this.cur_month);
		},
		onShow() {
			this.getdaka()
		},
		methods: {
			// 获取当月共多少天
			getThisMonthDays(year, month) {
				return new Date(year, month, 0).getDate()
			},
			// 获取当月第一天星期几
			getFirstDayOfWeek(year, month) {
				return new Date(Date.UTC(year, month - 1, 1)).getDay();
			},
			// 计算当月1号前空了几个格子，把它填充在days数组的前面
			calculateEmptyGrids(year, month) {
				//计算每个月时要清零
				this.days = [];
				const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
				if (firstDayOfWeek > 0) {
					for (let i = 0; i < firstDayOfWeek; i++) {
						var obj = {
							date: null,
							isSign: false
						}
						this.days.push(obj);
					}
				}
				// console.log(this.days)
			},
			// 绘制当月天数占的格子，并把它放到days数组中
			calculateDays(year, month) {
				const thisMonthDays = this.getThisMonthDays(year, month);
				for (let i = 1; i <= thisMonthDays; i++) {
					var obj = {
						date: i,
						isSign: false
					}
					this.days.push(obj);
				}
			},
			getdaka(newYear,newMonth) {
				this.zidingy = []
				let daysArr = this.days;
				if(newYear){
					for (var j = 0; j < daysArr.length; j++) {
						// 如果当前循环体小于等于31
						if (daysArr[j].date <= 31 && daysArr[j].date != null) {
							this.zidingy.push(daysArr[j])
						}
					}
				}else{
					for (var j = 0; j < daysArr.length; j++) {
						// 如果当前循环体小于当前天
						if (daysArr[j].date <= this.today && daysArr[j].date != null) {
							this.zidingy.push(daysArr[j])
						}
					}
				}
				this.zidingy = [...new Set(this.zidingy)]
				httpRequest.request("/api/user/task_log", 'POST', {
					year:this.cur_year,
					month:this.cur_month,
					n: this.zidingy.length,
				}, false, false, true).then(res => {
					let obj = res.data
					let arr = []
					for (let key in obj) {
						let date_s = key.split('-')
						let dict = {
							type: obj[key],
							t: parseInt(date_s[2])
						}
						arr.push(dict);
					}
					if(this.toYear==this.cur_year&&this.toMonth==this.cur_month){
						for (var j = 0; j < arr.length; j++) {
							// 如果当前循环体等于当前天并且type值为2，当前天已经打过卡
							if (arr[j].t == this.today&&arr[j].type==2){
								this.is_daka = true
							}
						}
					}
					this.SignUp = arr
					this.onJudgeSign(this.SignUp)
				})
			},
			//匹配判断当前月份哪些日子未打卡
			onJudgeSign(date) {
				var signs = date;
				var daysArr = this.days;
				// console.log(daysArr)
				//日期相同，当天
				for (var i = 0; i < signs.length; i++) {
					for (var j = 0; j < daysArr.length; j++) {
						// 如果当天循环内容的值等于当前天
						// if (daysArr[j].date == this.today) {
						// 	daysArr[j].isSign = 3;
						// }
						// 如果当前循环体大于当前天赋值为4禁用
						if(this.toMonth==this.cur_month&&this.toYear==this.cur_year){
							if (daysArr[j].date > this.today) {
								daysArr[j].isSign = 4;
							}
						}
						if (daysArr[j].date == signs[i]['t']) {
							if (signs[i]['type'] == 1) {
								// 今日可打卡
								daysArr[j].isSign = 3;
							} else if(signs[i]['type'] == 2) {
								// 已打卡
								daysArr[j].isSign = false;
							}else if(signs[i]['type'] == 0){
								// 可补卡
								daysArr[j].isSign = 2;
							}else if(signs[i]['type'] == "-1"){
								// 不可补卡
								daysArr[j].isSign = 5;
							}
						}
					}
				}
				this.days = daysArr;
				//网络请求
				// this.getdaka();
			},
			// 切换控制年月，上一个月，下一个月
			handleCalendar(type) {
				const cur_year = parseInt(this.cur_year);
				const cur_month = parseInt(this.cur_month);
				var newMonth;
				var newYear = cur_year;
				
				if (type === 0) { //上个月
					newMonth = cur_month - 1;
					if (newMonth < 1) {
						newYear = cur_year - 1;
						newMonth = 12;
					}
				} else {
					if(this.toMonth==cur_month&&this.toYear==cur_year){
						return false
					}
					newMonth = cur_month + 1;
					if (newMonth > 12) {
						newYear = cur_year + 1;
						newMonth = 1;
					}
				}
				this.calculateEmptyGrids(newYear, newMonth);
				this.calculateDays(newYear, newMonth);
				this.cur_year = newYear;
				this.cur_month = newMonth;
				this.getdaka(newYear,newMonth);
				this.SignUp = []; //清空
				this.$emit('dateChange', this.cur_year + "-" + this.cur_month); //传给调用模板页面去拿新数据				
			},
			//点击日期
			clickSignUp(t) {
				// console.log(t,"点击的天")
				// console.log(this.days,"所有数据")
				// 2,不可补卡  3,今天 4，今日日期之后  5,可补卡  false,打卡正常
				if (t.isSign == 5 || t.isSign == 3 || t.isSign == 4) {
					return
				} else if (t.isSign == 2) {
					// 补卡开始打卡
					let date = this.zhuanhdate(t)
					uni.navigateTo({
						url: '/pages/homepage/xuanzedakanr?date=' + date
					})
				} else if (t.isSign == false) {
					let date = this.zhuanhdate(t)
					uni.navigateTo({
						url: '/pages/homepage/dakalishi?date=' + date
					})
				}
			},
			// 当前日期开始打卡
			startdaka() {
				let date = this.date(new Date().valueOf())
				uni.navigateTo({
					url: "/pages/homepage/xuanzedakanr?date=" + date
				})
			},
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day
				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
			// 转换日期
			zhuanhdate(t) {
				let nian = this.cur_year
				let yue = ""
				let ri = ""
				if (this.cur_month <= 9) {
					yue = "0" + this.cur_month
				} else {
					yue = this.cur_month
				}
				if (t.date <= 9) {
					ri = "0" + t.date
				} else {
					ri = t.date
				}
				return nian + "-" + yue + "-" + ri
			}
		}
	}
</script>

<style lang="scss" scoped>
	.dibu {
		padding: 30rpx 30rpx 60rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.lvyuan {
		width: 33rpx;
		height: 33rpx;
		background: #61F58B;
		border-radius: 50%;
	}

	.huangyuan {
		width: 33rpx;
		height: 33rpx;
		background: #F57B61;
		border-radius: 50%;
	}

	.huiyuan {
		width: 33rpx;
		height: 33rpx;
		background: #999999;
		border-radius: 50%;
	}

	.bktext {
		margin: 0 30rpx 0 11rpx;
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999;
	}

	.all .bar {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 10rpx;
	}

	.bar .barbtn {
		height: 30px;
		line-height: 30px;
		font-size: 12px;
	}

	/* 星期 */
	.all .week {
		display: flex;
		/* flex-direction: row; */
		/* justify-content: space-between; */
		padding: 20rpx;
		border-radius: 10px;
		background-color: #fff;
		width: 90%;
		margin-left: 2%;
	}

	.week_ri {
		margin-left: 19rpx;
		padding: 0 48rpx 0 0;
	}

	.myDateTable {
		margin: 2.5vw;
		border-radius: 10px;
		background: #fff;
	}

	.myDateTable .dateCell {
		width: 11vw;
		padding: 1vw;
		display: inline-block;
		text-align: center;
		font-size: 16px;
	}

	.dateCell .cell {
		display: flex;
		border-radius: 50%;
		width: 59rpx;
		height: 59rpx;
		justify-content: center;
		align-items: center;
	}

	.greenColor {
		color: #01b90b;
		font-weight: bold;
	}

	.bgWhite {
		background-color: #fff;
	}

	.bgGray {
		background-color: rgba(255, 255, 255, 0.42);
	}

	.bgBlue {
		font-size: 14px;
		background-color: #4b95e6;
	}

	.redColor {
		color: #ff0000;
	 
 
	}

	.TipArea {
		word-break: break-all;
		word-wrap: break-word;

		font-size: 14px;
		padding: 10px;
	}

	.impTip {
		display: inline-block;
		color: #ff0000;
	}
	.huanse{
		background-color: #999;
		color: #fff;
	}
</style>
